package org.example.blogserver.service.impl;

import org.example.blogserver.dao.SysUserDao;
import org.example.blogserver.entity.SysUser;
import org.example.blogserver.handler.TokenManager;
import org.example.blogserver.service.LoginService;
import org.example.blogserver.service.SysUserService;
import org.example.blogserver.vo.Result;
import org.example.blogserver.vo.params.LoginParam;
import org.example.blogserver.vo.params.RegisterParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysUserDao sysUserDao;

    @Autowired
    private TokenManager tokenManager;

    @Override
    public Result login(LoginParam loginParam) {
        String account = loginParam.getAccount();
        String password = loginParam.getPassword();
        if (account == null || password == null) {
            return Result.fail(400, "用户名和密码不能为空！");
        }

        // 获取用户信息
        Result loginResult = sysUserService.getUserInfo(account, password);
        if (loginResult.getCode()!=200) {
            return loginResult;
        }

        // 登录成功，生成 token 并返回
        String token = tokenManager.generateToken(account);
        String role = sysUserService.getUserVoByAccount(account).getRole();
        Map<String, String> data = new HashMap<>();
        data.put("token", token);
        data.put("role", role);
        return Result.success("登录成功！", data);
    }

    @Override
    public Result logout(String token) {
        // 首先验证 token 是否有效
        if (!tokenManager.isValidToken(token)) {
            return Result.fail(400, "无效的 token！");
        }

        // 执行退出登录操作
        tokenManager.deleteToken(token);
        return Result.success("退出成功！", null);
    }

    @Override
    public Result register(RegisterParam registerParam) {
        String account = registerParam.getAccount();
        String password = registerParam.getPassword();
        String nickname = registerParam.getNickname();
        if (account == null || password == null) {
            return Result.fail(400, "用户名和密码不能为空！");
        }

        // 检查用户是否已存在
        try {
            // 检查用户是否已存在
            SysUser existingUser = sysUserDao.findUserByAccount(account);
            System.out.println(existingUser);
            if (existingUser != null) {
                return Result.fail(400, "用户已存在！");
            }else {
                // 用户不存在，可以继续注册
                // 创建新用户
                SysUser newUser = new SysUser();
                newUser.setAccount(account);
                newUser.setPassword(password);
                newUser.setNickname(nickname);
                newUser.setAvatar(null);
                if (account.equals("13000000000")) {
                    newUser.setRole("admin");
                } else {
                    newUser.setRole("user");
                }
                // 插入新用户信息到数据库
                int result = sysUserDao.insertUser(newUser);
                if (result <= 0) {
                    return Result.fail(500, "注册失败，请稍后重试！");
                }

                // 注册成功，返回成功结果
                return Result.success("注册成功！", null);
            }
        } catch (EmptyResultDataAccessException e) {
            // 用户已存在，返回失败结果
            return Result.fail(400, "用户已存在！");
        }

    }

}
